#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <pthread.h>

#define LEFT 30000000
#define RIGHT 30000200
#define THRNUM (RIGHT - LEFT + 1)
#define N 3

static void *thread_primer(void *p);

struct NUM
{
    int i;
};



int main(int argc, char **argv)
{
    void *ptr;
    pid_t pid;
    int i;
    int err;
    struct NUM *p;
    pthread_t tid[THRNUM];
    for (i = LEFT; i <= RIGHT; i++)
    {
        p = malloc(sizeof(struct NUM *));
        p->i = i;
        err = pthread_create(tid + (i - LEFT), NULL, thread_primer, p);
        if (err > 0)
        {
            fprintf(stderr, "pthread create error\n");
            exit(1);
        }
    }

    for (i = LEFT; i <= RIGHT; i++)
    {
        pthread_join(tid[i - LEFT],ptr);
        free(ptr);
    }
    exit(0);
}

static void *thread_primer(void *p)
{
    int i,mark, j ;

    i = ((struct NUM*) p)->i;
    for (j = 2; j < i / 2; j++)
    {
        mark = 1;
        if (i % j == 0)
        {
            mark = 0;
            break;
        }
    }

    if (mark)
    {
        printf("%d is a primer\n",  i);
    }

    pthread_exit(p);
}
